//
//  BBScreenSelectionView.m
//  BBTouch
//
//  Created by ben smith on 8/5/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import "BBScreenSelectionView.h"
#import "BBConfigurationController.h"


@implementation BBScreenSelectionView

@synthesize chosenScreen;
@synthesize scaleTransform;
@synthesize moveTransform;

-(void)awakeFromNib
{
	if ([BBConfigurationController objectForSettingKey:@"BBDefaultScreenNumber"] == nil) 
		self.chosenScreen = [NSScreen mainScreen]; // this is the default

	NSNumber * defaultNum =	[BBConfigurationController objectForSettingKey:@"BBDefaultScreenNumber"];
	self.chosenScreen = [BBScreenSelectionView screenWithNumber:defaultNum];
	[self calculate];
}


- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

-(void)setChosenScreen:(NSScreen*)aScreen
{
	[aScreen retain];
	[chosenScreen release];
	chosenScreen = aScreen;
	[BBConfigurationController setObject:[[aScreen deviceDescription] objectForKey:@"NSScreenNumber"] forSettingKey:@"BBDefaultScreenNumber"];
}

- (void)calculate
{
	NSRect screenBoundingRect = NSZeroRect;
	for (NSScreen * screen in [NSScreen screens]) {
		screenBoundingRect = NSUnionRect([screen frame], screenBoundingRect);
	}
	// calculate the scale to draw these rectangles at
	float scale;
	if (NSWidth(screenBoundingRect) > NSHeight(screenBoundingRect)) {
		scale = (NSWidth([self bounds]) - 10)/NSWidth(screenBoundingRect);
	} else {
		scale = (NSHeight([self bounds]) - 10)/NSHeight(screenBoundingRect);		
	}
	NSAffineTransform * scaleMe = [NSAffineTransform transform];
	[scaleMe scaleBy:scale];
	[self setScaleTransform:scaleMe];
	
	screenBoundingRect = [self transformRect:screenBoundingRect withTransform:scaleTransform];
	NSAffineTransform * moveMe = [NSAffineTransform transform];
	[moveMe translateXBy:(NSMidX([self bounds]) - NSMidX(screenBoundingRect) + 3.5) yBy:(NSMidY([self bounds]) - (NSHeight(screenBoundingRect)/2.0))];
	[self setMoveTransform:moveMe];
	
}

- (void)drawRect:(NSRect)rect {
    // Drawing code here.
	
	// not terribly efficient, but that is ok for now
	[moveTransform concat];
	[NSBezierPath setDefaultLineWidth:1.0];
	for (NSScreen * screen in [NSScreen screens]) {
		[[NSColor blackColor] set];
		[NSBezierPath strokeRect:[self transformRect:[screen frame] withTransform:scaleTransform]];
		if (screen == self.chosenScreen) {
			[[NSColor greenColor] set];			
		} else{
			[[NSColor colorWithDeviceWhite:0.8 alpha:1.0] set];
		}
		[NSBezierPath fillRect:[self transformRect:[screen frame] withTransform:scaleTransform]];
	}
}


-(void)mouseDown:(NSEvent*)theEvent
{
	NSPoint mouseDown = [[[self window] contentView] convertPoint:[theEvent locationInWindow] toView:self];
	//TODO; make this not a dupe of code	
	for (NSScreen * screen in [NSScreen screens]) {
		NSRect hitRect = [self transformRect:[screen frame] withTransform:scaleTransform];
		hitRect = [self transformRect:hitRect withTransform:moveTransform];
		if (NSPointInRect(mouseDown, hitRect)) {
			self.chosenScreen = screen;
			[self setNeedsDisplay:YES];
			return;
		}
	}
}

-(NSRect)transformRect:(NSRect)r withTransform:(NSAffineTransform*)xform
{
	NSPoint p = [xform transformPoint:r.origin];
	NSSize  s = [xform transformSize:r.size];
	return NSMakeRect(p.x, p.y, s.width, s.height);
}

+(NSScreen*)screenWithNumber:(NSNumber*)deviceID
{
	for (NSScreen * screen in [NSScreen screens]) {
		if ([[[screen deviceDescription] objectForKey:@"NSScreenNumber"] intValue] == [deviceID intValue]) {
			return screen;
		}
	}
	// default if i cant find it
	return [NSScreen mainScreen];
}

@end
